package com.rtp2p.rtcoresdkdemo.replay

import android.app.Application
import android.content.Context
import androidx.lifecycle.MutableLiveData
import com.rtp2p.rtcoresdkdemo.base.BaseViewModel
import com.rtp2p.rtnetworkcamera.media.render.RTVideoView
import com.rtp2p.rtp2pcore.common.RTCallback
import com.rtp2p.rtp2pcore.core.TFCardMediaSource
import com.rtp2p.rtp2pcore.core.DeviceFacade
import com.rtp2p.rtp2pcore.core.MediaRender
import com.rtp2p.rtp2pcore.core.TFCardRecordsRequest
import com.rtp2p.rtp2pcore.utils.RTLogUtils
import com.rtp2p.rtp2pcore.utils.RTTimeUtils

class ReplayViewModel(application: Application) : BaseViewModel(application) {
    //是否是全屏
    val fullscreen = MutableLiveData<Boolean>().apply { value = false }
    val loading = MutableLiveData<Boolean>().apply { value = false }
    val playStatus = MutableLiveData<String>().apply { value = "" } //ture:正在播放 false:停止播放
    val ptsString = MutableLiveData<String>().apply { value = "" }

    private var callback: RTCallback<List<TFCardRecordsRequest.RecordFile>>? = null
    private val recordFiles = mutableListOf<TFCardRecordsRequest.RecordFile>()
    private var online = false

    private var device: DeviceFacade? = null

    private var mediaSource: TFCardMediaSource? = null
    private var mediaRender: MediaRender? = null
    private var isReplay: Boolean = false

    private var recordsRequest: TFCardRecordsRequest? = null
    fun init(context: Context,deviceId: String,isAp :Boolean, videoView: RTVideoView, callback: RTCallback<List<TFCardRecordsRequest.RecordFile>>) {
        this.callback = callback;
        device = DeviceFacade(deviceId,isAp)

        mediaSource = TFCardMediaSource(device!!).apply {
            onReplayCallback = object : TFCardMediaSource.OnReplayCallback {
                override fun pts(pts: Long) {
                    ptsString.value = RTTimeUtils.getPTZByTime(pts)
                }

                override fun start() {
                    RTLogUtils.d("start")
                    playStatus.value = "暂停"
                    isReplay = true
                }

                override fun complete() {
                    RTLogUtils.d("complete")
                    playStatus.value = "播放"
                    isReplay = false
                }
            }
        }
        mediaRender = MediaRender(context,videoView, mediaSource!!)

        recordsRequest = TFCardRecordsRequest(device!!)

        device?.setConnectStateListener{
            RTLogUtils.d("连接状态 state :${it.toString()}")
            online = DeviceFacade.Status.Connected == it
            if (online) {
                if (recordFiles.isNotEmpty()) recordFiles.clear()
                loadRecords()
            }
        }
    }

    internal fun onStart() {
        if (online) {
            if (recordFiles.isNotEmpty()) recordFiles.clear()
            loadRecords()
        } else {
            connect()
        }
    }

    internal fun onStop() {
        pause()
        //disconnect()
    }

    private fun connect() {
        device?.connect()
    }

    private fun disconnect() {
        device?.disconnect()
    }

    private var currentRecordFile: TFCardRecordsRequest.RecordFile? = null
    fun btnPlay(context: Context) {
        if (isReplay)
            pause()
        else
            this.currentRecordFile?.let { play(it) }
    }

    //开始播放
    fun play(recordFile: TFCardRecordsRequest.RecordFile) {
        this.currentRecordFile = recordFile;
        mediaRender?.play()
        mediaSource?.play()
    }

    //停止播放
    private fun pause() {
        mediaSource?.pause()
        mediaRender?.pause()
    }

    private var recordPageIndex = -1
    private var year = 2024
    private var month = 11
    private var day = 22

    internal fun loadRecords() {
        recordPageIndex++
        loading.value = true;
        recordsRequest?.queryRecords(year, month, day, recordPageIndex) {
            loading.value = false
            if (it.success) {
                recordFiles.addAll(it.data.records)
                callback?.accept(recordFiles)
            } else {
                RTLogUtils.d("loadRecords code:${it.code} msg:${it.message}")
            }
        }
    }
}